const path = require('path')
const {defineConfig, build} = require('vite')
const vue = require('@vitejs/plugin-vue')
const vueJsx = require('@vitejs/plugin-vue-jsx')
const scss = require('rollup-plugin-scss')
const dts = require('vite-plugin-dts').default

//入口文件夹
const entryDir = path.resolve((__dirname, "./src/framework"))
//出口文件夹
const outDir = path.resolve((__dirname, "./lib"))

//vite配置
const baseConfig = defineConfig({
    configFile: false,
    publicDir: false,
    plugins: [
        vue(),
        vueJsx(),
        scss({
            output: path.resolve(outDir, 'style.css'),
            include: ['./src/**/*.scss']
        }),
        dts({
            outDir: path.resolve(outDir, 'dist'),
            insertTypesEntry: true,
            entryRoot: './src/framework',
            include: [
                "src/**/*.d.ts",
                "src/**/*.ts",
                "src/**/*.tsx",
                "src/**/*.vue",
                "src/**/*.scss",
                "src/**/*.js",
                "src/**/*.jsx"
            ]
        })
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "@/style/reset.scss";`
            }
        }
    },
    resolve: {
        alias: {
            '@': path.resolve(entryDir)
        }
    }
})

//rollup配置
const rollupOptions = {
    external: ['vue', 'vue-router'],
    output: {
        globals: {
            vue: 'Vue'
        }
    }
}

//全量打包构建方法
const buildAll = async () => {
    await build({
        ...baseConfig,
        build: {
            rollupOptions: {
                ...rollupOptions,
                // 处理.d.ts文件
                plugins: [
                    ...rollupOptions.plugins || []
                ]
            },
            lib: {
                entry: path.resolve(entryDir, "index.ts"),
                name: "AnQinFrameWork",
                fileName: "AnQinFrameWork",
                formats: ['es', 'umd']
            },
            outDir: outDir,
            emptyOutDir: false,
            cssCodeSplit: false
        }
    })
}

const buildLib = async () => {
    await buildAll()
}

buildLib()